<?php
namespace App\Master\Framework\Extend;

class Verify {

    public static function identityCard($str)
    {
        if(empty($str) || strlen($str)!=18){
            return false;
        }
        $card_base = substr($str, 0, 17);
        $verify_code = substr($str, 17, 1);
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        $verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        $total = 0;
        for( $i = 0; $i < 17; $i ++ ){
            $total += substr($card_base, $i, 1) * $factor[$i];
        }
        $mod = $total % 11;
        if($verify_code == $verify_code_list[$mod]){
            return true;
        }else{
            return false;
        }
    }

    public static function email($email){
        if (empty($email)) {
            return false;
        }
        return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
    }

    public static function account($str, $min = 6, $max = 20){
        if (empty($str)) {
            return false;
        }
        $len = mb_strlen($str, 'utf-8');
        if ($len < $min || $len > $max){
            return false;
        }
//        $reg = '/^[a-zA-Z0-9_\x80-\xff]{'.$min.','.$max.'}$/';
        $reg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-\.,: ]{'.$min.','.$max.'}$/u';
        return preg_match($reg, $str);
    }

    public static function string($str, $min = 6, $max = 20){
        if (empty($str)) {
            return false;
        }
        $len = mb_strlen($str, 'utf-8');
        if ($len < $min || $len > $max){
            return false;
        }
        $reg = '/^[a-zA-Z0-9_\x80-\xff]{'.$min.','.$max.'}$/';
        return preg_match($reg, $str);
    }

    public static function password($str, $min = 6, $max = 20){
        if (empty($str)) {
            return false;
        }
        $len = mb_strlen($str, 'utf-8');
        if ($len < $min || $len > $max){
            return false;
        }
//        $reg = '/^[a-zA-Z0-9_]{'.$min.','.$max.'}$/';
        $reg = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_\-\.,: ]{'.$min.','.$max.'}$/u';
        return preg_match($reg, $str);
    }
    
    public static function phone($str){
        if (empty($str)) {
            return false;
        }
        $len = strlen($str);
        if ($len < 11 || $len > 11 || !preg_match("/1[3|4|5|6|7|8|9]{1}[0-9]{9}$/", $str))
        {
            return false;
        }
        return true;
    }
}
